home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gp_itbc.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  7KB  |  223 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_itbc.c */
  20. /* Intel processor, Turbo/Borland C-specific routines for Ghostscript */
  21. #include "dos_.h"
  22. #include <fcntl.h>
  23. #include <io.h>
  24. #include <signal.h>
  25. #include "string_.h"
  26. #include "gx.h"
  27. #include "gp.h"
  28. #ifdef __OVERLAY__
  29. #  include "overlay.h"
  30. #endif
  31.  
  32. /* Library routines not declared in a standard header */
  33. extern char *getenv(P1(const char *));
  34.  
  35. /* Define the size of the C stack. */
  36. unsigned _stklen = 8000;        /* default is 4096, we need more */
  37.  
  38. /* Define the size of the overlay buffer, if relevant. */
  39. #ifdef __OVERLAY__
  40. unsigned _ovrbuffer = (1024L * OVLBUFK) / 16;
  41. #endif
  42.  
  43. /* Forward declarations */
  44. private void handle_FPE(P3(int, int, int *));
  45.  
  46. /* Do platform-dependent initialization. */
  47. #if CPU_TYPE > 86
  48. /* Internal routine to set flags and read them back. */
  49. /* We use __emit__ so we don't require an assembler. */
  50. private int
  51. push_pop_flags(unsigned flags)
  52. {    __emit__(0x8b, 0x46, 6);    /* mov ax,flags */
  53.     __emit__(0x50, 0x9d);        /* push ax; popf */
  54.     __emit__(0x9c, 0x58);        /* pushf; pop ax */
  55. }
  56. #endif
  57. void
  58. gp_init(void)
  59. {    /*
  60.      * Detect the processor type using the following algorithms:
  61.      *    The 8088/8086 truncate shift counts mod 32,
  62.      *      the 80186 and up do not.
  63.      *    The 80186 and below fix FLAGS bits 15-12 to 1,
  64.      *      the 80286 and up do not.
  65.      *    The 80386 allows setting FLAGS bits 14-12,
  66.      *      the 80286 and below do not.
  67.      * We currently can't tell an 80386 from an 80486.
  68.      * Note that this algorithm will identify an 80386
  69.      *   running in Virtual 8086 mode as an 80386.
  70.      *   This is acceptable, because Ghostscript doesn't actually
  71.      *   use 80286 or 80386 addressing modes, only the additional
  72.      *   instructions available on these processors.
  73.      * (This algorithm is derived from the Intel manuals.)
  74.      */
  75. #if CPU_TYPE > 86
  76.     /* We have to be careful not to turn interrupts off! */
  77.     int result, type;
  78.     result = push_pop_flags(0x202);
  79.     if ( (result & 0xf000) == 0xf000 )
  80.        {    /* CPU is an 8088/8086/80186 */
  81.            {    int shc = 33;    /* force shift by variable */
  82.             result = 0xffff << shc;
  83.            }
  84.         type = (result == 0 ? 186 : 86);
  85.        }
  86.     else
  87.        {    /* CPU is an 80286/80386/... */
  88.         result = push_pop_flags(0x7202);
  89.         type = ((result & 0x7000) == 0 ? 286 : 386);
  90.        }
  91.     /* A 486 is the same as a 386. */
  92. #define CPU_EQUIV (CPU_TYPE == 486 ? 386 : CPU_TYPE)
  93.     if ( type < CPU_EQUIV )
  94.        {    eprintf1("This executable requires an 80%d or higher.\n",
  95.              CPU_EQUIV);
  96.         exit(1);
  97.        }
  98. #endif
  99.  
  100. #ifdef __OVERLAY__
  101.     /* Initialize the overlay machinery. */
  102.        {    int code;
  103. #  ifdef OVEMS
  104.         code = _OvrInitEms(OVEMS_HANDLE, OVEMS_FIRST, OVEMS_PAGES);
  105.         if ( code )
  106.             eprintf("Attempt to use EMS memory for overlays failed.\n");
  107. #  endif
  108. #  ifdef OVEXT
  109.         code = _OvrInitExt(OVEXT_START, OVEXT_LENGTH);
  110.         if ( code )
  111.             eprintf("Attempt to use extended memory for overlays failed.\n");
  112. #  endif
  113.        }
  114. #endif
  115.     /* Set up the handler for numeric exceptions. */
  116.     signal(SIGFPE, handle_FPE);
  117.     gp_init_console();
  118. }
  119.  
  120. /* Trap numeric exceptions.  Someday we will do something */
  121. /* more appropriate with these. */
  122. private void
  123. handle_FPE(int sig, int subcode, int *regs)
  124. {    unsigned char far *ip = MK_PTR(regs[10], regs[9]);
  125.     eprintf1("Numeric exception %d:\n", subcode);
  126.     fprintf(estderr,
  127. "AX=%04x  BX=%04x  CX=%04x  DX=%04x  SI=%04x  DI=%04x  BP=%04x\n",
  128.         regs[8], regs[7], regs[6], regs[5], regs[2], regs[1], regs[0]);
  129.     fprintf(estderr,
  130. "DS=%04x  ES=%04x  CS:IP=%04x:%04x",
  131.         regs[3], regs[4], regs[10], regs[9]);
  132.     fflush(estderr);        /* in case of trap */
  133.     fprintf(estderr, " --> %02x %02x %02x %02x %02x >> %02x %02x %02x %02x %02x\n",
  134.         ip[-5], ip[-4], ip[-3], ip[-2], ip[-1],
  135.         ip[0], ip[1], ip[2], ip[3], ip[4]);
  136.     exit(1);
  137. }
  138.  
  139. /* Do platform-dependent cleanup. */
  140. void
  141. gp_exit(int exit_status, int code)
  142. {
  143. }
  144.  
  145. /* ------ Printer accessing ------ */
  146.  
  147. /* Open a connection to a printer.  A null file name means use the */
  148. /* standard printer connected to the machine, if any. */
  149. /* Return NULL if the connection could not be opened. */
  150. extern void gp_set_printer_binary(P2(int, int));
  151. FILE *
  152. gp_open_printer(char *fname, int binary_mode)
  153. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  154.     {    if ( binary_mode )
  155.             gp_set_printer_binary(fileno(stdprn), 1);
  156.         return stdprn;
  157.     }
  158.     else
  159.         return fopen(fname, (binary_mode ? "wb" : "w"));
  160. }
  161.  
  162. /* Close the connection to the printer. */
  163. void
  164. gp_close_printer(FILE *pfile, const char *fname)
  165. {    if ( pfile != stdprn )
  166.         fclose(pfile);
  167. }
  168.  
  169. /* ------ File names ------ */
  170.  
  171. /* Create and open a scratch file with a given name prefix. */
  172. /* Write the actual file name at fname. */
  173. FILE *
  174. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  175. {    char *temp;
  176.     if ( (temp = getenv("TEMP")) == NULL )
  177.         *fname = 0;
  178.     else
  179.     {    char last = '\\';
  180.         strcpy(fname, temp);
  181.         /* Prevent X's in path from being converted by mktemp. */
  182.         for ( temp = fname; *temp; temp++ )
  183.             *temp = last = tolower(*temp);
  184.         switch ( last )
  185.         {
  186.         default:
  187.             strcat(fname, "\\");
  188.         case ':': case '\\':
  189.             ;
  190.         }
  191.     }
  192.     strcat(fname, prefix);
  193.     strcat(fname, "XXXXXX");
  194.     mktemp(fname);
  195.     return fopen(fname, mode);
  196. }
  197.  
  198. /* ------ File operations ------ */
  199.  
  200. /* If the file given by fname exists, fill in its status and return 1; */
  201. /* otherwise return 0. */
  202. int
  203. gp_file_status(const char *fname, file_status *pstatus)
  204. {    FILE *f = fopen(fname, "r");
  205.     long flen;
  206.     struct ftime ft;
  207.     if ( f == NULL ) return 0;
  208.     if ( getftime(fileno(f), &ft) < 0 )
  209.        {    fclose(f);
  210.         return 0;
  211.        }
  212.     fseek(f, 0, SEEK_END);
  213.     flen = ftell(f);
  214.     pstatus->size_pages = (flen + 1023) >> 10;
  215.     pstatus->size_bytes = flen;
  216.     /* Make a single long value from the ftime structure. */
  217.     pstatus->time_referenced = pstatus->time_created =
  218.       ((long)((ft.ft_year << 9) + (ft.ft_month << 5) + ft.ft_day) << 16) +
  219.       ((ft.ft_hour << 11) + (ft.ft_min << 5) + ft.ft_tsec);
  220.     fclose(f);
  221.     return 1;
  222. }
  223.